<template>
    <div class="app-container">
      <!--<h4>新闻管理</h4>-->
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%; margin-bottom: 25px;">
        <el-table-column
          prop="indexSy"
          width="50"
          label="序号">
        </el-table-column>
        <el-table-column
          prop="author"
          label="作者"
          width="100">
        </el-table-column>
        <el-table-column
          label="标题">
          <template slot-scope="scope">
            <div class="txt-break" :title="scope.row.title">
                {{scope.row.title}}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="updata_time"
          label="更新日期"
          width="180">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="是否发布"
          width="80">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              @change="switchState(scope.row)"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#eeeeee">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="置顶"
          width="80">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_top"
              @change="isTopState(scope.row)"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#eeeeee">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="130">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row.id_number)" type="text" size="small">查看</el-button>
            <el-button type="text" size="small" @click="editArl(scope.row.id_number)">编辑</el-button>
            <el-button type="text" size="small" @click="deleteArl(scope.row.id_number)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        layout="prev, pager, next"
        @current-change="pageChange"
        :total="total">
      </el-pagination>

      <info-art
        v-if="visible"
        :artID="articleID"
        :IsdialogVisible.sync="visible"></info-art>

    </div>
</template>

<script lang="ts">
  import { articleList, articleDelet, EditArticle } from '../../api/data'
  import { format } from '../../utils/validate'
  import infoArt from './page/infoArt.vue'
  import Vue from 'vue'
  import Component from 'vue-class-component'

  @Component({
    components:{
      infoArt
    }
  })

  export default class sysNew extends Vue {

    loading:boolean = false;
    tableData:any[] = [];
    getListPosd ={
      pagesize:10,
      page:1,
    };
    total:number = 0;
    visible:boolean = false;
    articleID:string = '';

    mounted(){
      this.anLoad()
    };
    anLoad(){
      this.getList();
    };

    getList(){
      let nd = this.getListPosd;
      this.loading = true;
      articleList(nd).then((res:any)=>{
        this.loading = false;
        let nwd = res.data;
        let putlist:any[] = [];
        nwd.list.filter((item:any,i:number)=>{
          item.indexSy = format(i,this.getListPosd.page);
          putlist.push(item)
        });
        this.total = nwd.total;
        this.tableData = putlist;
      })
    };

    pageChange(val:number){    //分页
      this.getListPosd.page = val;
      this.getList();
    };

    handleClick(id:string){    //查看
      this.visible = true;
      this.articleID = id;
    };

    editArl(id:string){     //编辑
      let url = 'addnew?id='+ id;
      this.$router.push({path:url})
    };

    deleteArl(id:string){ //删除
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        articleDelet(id).then((res:any)=>{
          this.$message.success('删除成功！');
          this.getList();
        })
      }).catch((err:any)=>{console.log(err);});
    };

    switchState(val:any){    //是否发布
      let nd = {
        id_number:val.id_number,
        state:val.state,
      };
      EditArticle(nd).then((res:any)=>{})
    };

    isTopState(val:any){
      let nd = {
        id_number:val.id_number,
        is_top:val.is_top,
      };
      EditArticle(nd).then((res:any)=>{})
    };

  }

</script>

<style scoped>

</style>
